/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModuloConfiguracion;

import Modelo.DAL.Area_DAO;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;

/**
 *
 * @author andres
 */
public class listaAreaAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String nombre_sede = request.getParameter("nombre_sede");
        String id_area = request.getParameter("id_area");
        String descripcion = request.getParameter("descripcion");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");

        String where = "SELECT *,(Select count(*) from area) FROM area where 1 = 1 ";

        if (nombre_sede != null && !nombre_sede.equals("")) {
            where += " and nombre_sede = '" + nombre_sede + "'";
        }

        if (id_area != null && !id_area.equals("")) {
            where += " and id_area = '" + id_area + "'";
        }


        if (descripcion != null && !descripcion.equals("")) {
            where += " and descripcion = '" + descripcion + "'";
        }
        where += "AND estado_area = 'on'";
        where += " ORDER BY descripcion";
        if (limit != null && !limit.equals("0")) {
            where += " limit " + limit + " offset " + offset;
        }
        response.setContentType("text/json");
        JSONObject ext = new JSONObject();
        Area_DAO aDao = new Area_DAO();
        ext.put("consultarArea1", aDao.consultarAreaTabla(where));
        PrintWriter pw = response.getWriter();
        pw.write(ext.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
